package com.lvyq.myplayer.ui.activity.ranking

import android.content.Intent
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.current.CurrentPlayLstAdapter
import com.lvyq.myplayer.adapter.ranking.TheCharItemAdapter
import com.lvyq.myplayer.base.BaseActivity
import com.lvyq.myplayer.common.shared.MainAudioSharedPrefsManager
import com.lvyq.myplayer.common.util.DataOperationUtils
import com.lvyq.myplayer.common.util.StartActivityUtils
import com.lvyq.myplayer.model.ranking.RankingItemModel
import com.lvyq.myplayer.parcelable.SongInfoData
import com.lvyq.myplayer.service.AudioService
import com.lvyq.myplayer.ui.activity.AudioPlayerActivity
import com.lvyq.myplayer.util.ThreadUtil
import com.squareup.picasso.Picasso
import org.jetbrains.anko.toast

/**
 * 文件描述：TheCharItemActivity
 * 作者：lvyq
 * 创建时间：2025-06-03
 * 修改时间：2025-06-03
 */
class TheCharItemActivity: BaseActivity() {

    lateinit var viewRankingItemModel: RankingItemModel
    override var currentPlayAdapter: CurrentPlayLstAdapter? = null

    lateinit var songLstView:RecyclerView
    val rankingAdapter by lazy { getAdapter() }


    lateinit var bannerImgView:ImageView
    override fun getLayoutId(): Int {
       return  R.layout.activity_index_daily
    }

    override fun initData() {
        initPlayBoxService()
        bannerImgView =findViewById<ImageView>(R.id.bannerImg)
        songLstView =findViewById<RecyclerView>(R.id.dailyLst)

        songLstView.layoutManager = LinearLayoutManager(this)

       var rankingId= intent.getIntExtra("rankingId",-1)

        //背景图
        var bannerImg = intent.getStringExtra("bannerImg")

        viewRankingItemModel=ViewModelProvider(this)[RankingItemModel::class.java]
        viewRankingItemModel.data.observe(this){ data->
            songLstView.adapter=rankingAdapter
            rankingAdapter.setData(data.data)
        }
        viewRankingItemModel.list(rankingId)

        //背景
        Picasso.with(this).load(bannerImg).into(bannerImgView)
    }


    fun getAdapter(): TheCharItemAdapter {
        return TheCharItemAdapter(this,object :
            TheCharItemAdapter.OnItemClickListener{
            override fun onItemClick(position: Int, song: SongInfoData,list:ArrayList<SongInfoData>) {

                StartActivityUtils.startAudioPlayerActivityAndSaveDB(this@TheCharItemActivity,list,position)
            }

            override fun onAddItemClick(position: Int, data: SongInfoData) {
                DataOperationUtils.addWithPositionAndReSet(this@TheCharItemActivity,data)
                ThreadUtil.runOnMainThread{
                    toast("添加到下一首")
                }
            }
        })
    }


    /**
     * 播放栏服务初始化
     */
    private fun initPlayBoxService() {
        initMainPlayBoxListener()
        currentPlayAdapter=getCurrentPlayLstAdapter()
        initAudioPlayLst(this)

        //设置viewpage2 适配器
        recreateViewPagerAdapter()
        initMainPlayStateChangeUI()
        // updateMainPlayStateBtn(false)
        var songInfo = MainAudioSharedPrefsManager.getSongInfo(this)
        var currentAudioPosition = MainAudioSharedPrefsManager.getCurrentAudioPosition(this)
        //通过audioservice 播放,混合启动，保证退出当前页仍然播放
        val audioIntent = Intent(this, AudioService::class.java)
        //传递值到AudioService中
        audioIntent.putExtra("data",songInfo)
        audioIntent.putExtra("from",-1)
        audioIntent.putExtra("position",currentAudioPosition?.toInt())
        //绑定 BIND_AUTO_CREATE 服务未创建时创建并绑定
        bindService(audioIntent,connection, BIND_AUTO_CREATE)
        //启动
        startService(audioIntent)
    }

    private fun getCurrentPlayLstAdapter(): CurrentPlayLstAdapter {
        return CurrentPlayLstAdapter(this)
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}